package com.tushen.labor.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tushen.labor.R;
import com.tushen.labor.bean.WeatherInfo;
import com.tushen.labor.util.DateUtil;
import com.tushen.labor.view.ViewAdjust;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class WeatherItemAdapter extends BaseAdapter {

    private static final String TAG = "WeatherItemAdapter";
    private LayoutInflater mInflater;
    private List<WeatherInfo> mList = null;

    public WeatherItemAdapter(LayoutInflater inflater) {
        mInflater = inflater;
    }

    @Override
    public int getCount() {

        if (mList == null) {
            Log.d(TAG, "getCount mList == null ...size = 0");
            return 0;
        }
        //Log.d(TAG, "getCount size = " + mList.size());
        return mList.size();
    }

    public void clear() {
        mList = null;
    }

    public void setList(List<WeatherInfo> list) {

        clear();
        if (list == null || list.size() <= 0) {
            notifyDataSetChanged();
            return;
        }
        mList = new ArrayList<>();
        mList.addAll(list);
        Log.d(TAG, "setList size = " + mList.size());
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static  boolean checkBoundIsOk(Collection c, int index){

        if(c == null || index < 0 || index >= c.size()){
            return false;
        }
        return true;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (!checkBoundIsOk(mList, position)) {
            return null;
        }

        HolderView holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.weather_item, parent, false);
            ViewAdjust.adjustLayoutParams(mInflater.getContext(), convertView.findViewById(R.id.inner_root_view));
            holder = new HolderView();
            holder.imageView = convertView.findViewById(R.id.item_image);
            holder.focusBgView = convertView.findViewById(R.id.focus_bg);
            holder.date = convertView.findViewById(R.id.item_text_date);
            holder.week = convertView.findViewById(R.id.item_text_week);
            holder.currTemperature = convertView.findViewById(R.id.item_text_curr_temperature);
            holder.temperatureRange = convertView.findViewById(R.id.item_text_temperature_range);
            holder.windDesc = convertView.findViewById(R.id.item_text_wind);
            holder.textWeather = convertView.findViewById(R.id.item_text_weather);
            convertView.setTag(holder);
        } else {
            holder = (HolderView) convertView.getTag();
        }

        //holder.imageView.setImageDrawable(new ColorDrawable(mColorList.get(position)));
        //Log.d(TAG,"getView pos =  "+position+" "+mList.get(position));

        WeatherInfo info = mList.get(position);
        holder.date.setText(info.getDateStr());
        holder.week.setText(info.getWeekStr());
        holder.textWeather.setText(info.getWeather().getDesc());

        String currTemperature = info.getCurrTemperature();
        if (currTemperature!= null && !"".equals(currTemperature)){
            holder.currTemperature.setText(currTemperature + "℃");
            holder.temperatureRange.setText("");
            holder.focusBgView.setVisibility(View.VISIBLE);
        }else{
            holder.currTemperature.setText("");
            holder.temperatureRange.setText(info.getTemperatureRange());
            holder.focusBgView.setVisibility(View.INVISIBLE);
        }

        Integer houtInt = Integer.parseInt(DateUtil.getNowHHStr());
        String prefix = houtInt>=18?"n":"d";
        String resId = prefix+info.getWeather().getCode();

        Class drawable = R.drawable.class;
        try {
            Field filed = drawable.getField(resId);
            int res_id = filed.getInt(filed.getName());
            holder.imageView.setImageResource(res_id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        holder.windDesc.setText(info.getWindDescription());
        return convertView;
    }


    private static class HolderView {
        //RoundedImageView
        ImageView imageView;
        ImageView focusBgView;
        TextView date;
        TextView week;
        TextView currTemperature;
        TextView temperatureRange;
        TextView windDesc;
        TextView textWeather;
    }

}
